package com.tedu.bill;

import java.util.Arrays;

/**
 * Author：毕龙
 * Date: 2024/3/31 11:21
 */
public class TestArrayExpand {
    public static void main(String[] args) {
        //数组复制
        int[] arr = {1,2,3,4,5};
        //调用函数
        int[] b = copyOrigin(arr);

        //数组的遍历
        System.out.println(Arrays.toString(b));
        b = copyRelative(arr);
        System.out.println(Arrays.toString(b));
        b = copyNative(arr);
        System.out.println(Arrays.toString(b));

    }
    //最原始复制
    public static int[] copyOrigin(int[] a){
        //新数组是源数组长度的2倍
        int[] b = new int[a.length*2];
        //将源数组的所有元素赋值给新数组
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        //返回新数组
        return b;
    }

    //系统提供功能
    public static int[] copyRelative(int[] a){

        //五个参数
        int[] b = new int[a.length*2];
        System.arraycopy(a,0,b, 0, a.length);
        return b;
    }
    //工具类提供的功能
    public static int[] copyNative(int[] a){
        //把系统提供的功能简化
        //要复制的源数组
        //复制新的长度
        /**
         * Params:
         * original – the array to be copied
         * newLength – the length of the copy to be returned
         */
        int[] copy = Arrays.copyOf(a, a.length * 2);
        return copy ;
    }
}
